<template>
  <view class="recharge">
    <view class="status-bar" :style="{ height: navHeight + 'px', 'padding-top': statusBarHeight + 'px' }">
      <view class="nav-bar">
        <image src="/static/left_arrow.png" class="nav-back-icon" @click="goBack" />
        <text class="nav-title">充值中心</text>
      </view>
    </view>
    <view class="status-height-color" :style="{ height: navHeight + 'px', 'padding-top': statusBarHeight + 'px' }">
    </view>


    <!-- 充值选项卡片区 -->
    <view class="recharge-options">
      <view class="recharge-row">
        <view class="recharge-card" v-for="(item, idx) in rechargeList" :key="idx"
          :class="{ 'selected': selectedIdx === idx }" @click="onCardClick(idx)">
          <view class="recharge-card-main">
            <image class="recharge-card-icon" :src="item.icon" mode="aspectFit" />
            <text class="recharge-card-amount">{{ item.amount }}</text>
          </view>
          <text class="recharge-card-yuan">{{ item.yuan }}</text>
        </view>
      </view>
    </view>

    <!-- 购买提示和按钮 -->
    <view class="buy-section">
      <view class="buy-tip">会员充值为权益专享服务，购买后不支持退换</view>
      <view class="buy-btn-wrap">
        <image class="buy-btn-bg" src="/static/buy_bg.png" mode="aspectFill" />
        <view class="buy-btn-text" @click="onBuy">确认购买</view>
      </view>
      <view class="buy-agree-row">
        <image class="buy-agree-icon" :src="agree ? '/static/ageress.png' : '/static/noagress_icon.png'"
          @click="toggleAgree" />
        <text class="buy-agree-text">同意</text>
        <text class="buy-agree-link" @click="onShowTerms">并继续 </text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      navHeight: "", //整体顶部导航栏的高度
      statusBarHeight: "", //状态栏高度

      rechargeList: [
        { icon: '/static/1y.png', amount: '299元', yuan: '包月' },
        { icon: '/static/1y.png', amount: '499元', yuan: '包季' },
        { icon: '/static/1y.png', amount: '1799元', yuan: '包年' },
      ],
      agree: false,
      customActive: false,
      customAmount: '',
      customDisplayAmount: '',
      selectedIdx: null,
      lottieSrc: '', // 动画文件路径
    }
  },
  onLoad() {
    let { statusBarHeight, system } = uni.getSystemInfoSync();
    this.statusBarHeight = statusBarHeight;
    this.navHeight = statusBarHeight + (system.indexOf("iOS") > -1 ? 44 : 48);
  },
  methods: {
    goBack() {
      uni.navigateBack();
    },

    onShowTerms() {
      // 展示条款弹窗或跳转
    },
    onCardClick(idx) {
      this.selectedIdx = idx;
    },
    onCustomClick(idx) {
      if (idx === 2) {
        this.selectedIdx = null;
        // 只有在未输入或未激活时才重置内容，否则保持输入和高亮
        if (!this.customActive && !this.customAmount && !this.customDisplayAmount) {
          this.customDisplayAmount = '';
          this.customAmount = '';
        }
        this.customActive = true;
        this.$nextTick(() => {
          // 自动聚焦输入框
        });
      }
    },
    onCustomBlur() {
      if (this.agree && this.customDisplayAmount) {
        this.customActive = false;
      }
    },
    onBuy() {
      if (this.selectedIdx === null) {
        uni.showToast({
          title: '请选择充值套餐',
          icon: 'none'
        });
        return;
      }
      if (!this.agree) {
        uni.showToast({
          title: '请勾选已阅读并同意条款',
          icon: 'none'
        });
        return;
      }
      // 获取当前选中的卡片信息
      const selectedCard = this.rechargeList[this.selectedIdx];
      console.log('你选中的卡片是：', selectedCard);
      // 这里继续处理购买逻辑，比如发起支付请求
    },
    toggleAgree() {
      this.agree = !this.agree;
    },

  }
}
</script>

<style lang="scss" scoped>
page {
  background-color: #100D05;
}

.recharge {
  background-color: #100D05;
  min-height: 100vh;
  padding: 0 30rpx;
}

.status-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
  background-color: #100D05;
  display: flex;
  align-items: center;
  color: #fff;
  font-weight: 500;
  font-size: 35rpx;

  .nav-bar {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;
    padding: 0 23rpx;
  }

  .nav-back-icon,
  .nav-details-icon {
    width: 46.15rpx;
    height: 46.15rpx;
  }

  .nav-title {
    flex: 1;
    text-align: center;
    font-size: 35rpx;
    font-weight: 500;
    color: #fff;
  }
}

.status-height-color {
  height: 44px;
  padding-top: 20px;
}

.balance-card {
  position: relative;
  width: 100%;
  height: 307rpx;
  border-radius: 24rpx;
  overflow: hidden;
  background: transparent;
  margin-top: 30rpx;
}

.lottie-wrapper {
  width: 100%;
  height: 307rpx;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
}

.balance-info {
  position: absolute;
  left: 60rpx;
  top: 50%;
  transform: translateY(-50%);
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.balance-amount {
  font-size: 62rpx;
  color: #fff;
  font-weight: 500;
  line-height: 1.2;
}

.balance-label {
  font-size: 31rpx;
  font-weight: 400;
  color: #fff;
  margin-top: 12rpx;
}

.feature-bar {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: flex-start;
  margin: 73rpx 0 0 0;
  padding: 0 0rpx;
}

.feature-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 120rpx;
}

.feature-icon-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
}

.feature-icon {
  width: 107.69rpx;
  height: 113.46rpx;
}

.feature-title {
  font-size: 24rpx;
  color: #fff;
  font-weight: 500;
  margin-bottom: 4rpx;
}

.feature-desc {
  font-size: 19rpx;
  color: #FFFFFF;
  opacity: 0.5;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 3rpx;
}

.recharge-options {
  margin: 73rpx 0 0 0;
  display: flex;
  flex-direction: column;
  gap: 18rpx;
}

.recharge-row {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  // gap: 32rpx;
}

.recharge-card {
  background: #2B2A3A;
  border-radius: 15.38rpx;
  width: 219rpx;
  height: 154rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  border: 4rpx solid transparent;
  transition: border 0.25s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.25s cubic-bezier(0.4, 0, 0.2, 1), background 0.2s;
}

.recharge-card.selected {
  border: 4rpx solid #4556CA;
  box-shadow: 0 0 8rpx 0 #4556CA33;
}

.recharge-card.custom-active {
  background: #4556CA;
}

.recharge-card-main {
  display: flex;
  align-items: center;
  margin-bottom: 12rpx;
}

.recharge-card-icon {
  width: 36.54rpx;
  height: 38.46rpx;
  margin-right: 12rpx;
}

.recharge-card-amount {
  font-size: 38rpx;
  color: #F8FBFF;
  font-weight: 400;
}

.recharge-card-yuan {
  font-size: 27rpx;
  color: rgba(255, 255, 255, 0.5);
  font-weight: 400;
}

.custom-input {
  width: 120rpx;
  height: 40rpx;
  background: transparent;
  border: none;
  outline: none;
  color: #fff;
  font-size: 27rpx;
  text-align: left;
  margin-top: 6rpx;
}

.custom-placeholder {
  color: rgba(255, 255, 255, 0.5);
  font-size: 27rpx;
  margin-top: 6rpx;
}

.buy-section {
  margin: 93rpx 0 0 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.buy-tip {
  color: rgba(255, 255, 255, 0.5);
  font-size: 23rpx;
  margin-bottom: 27rpx;
  text-align: center;
}

.buy-btn-wrap {
  position: relative;
  width: 634.62rpx;
  height: 92.31rpx;
  margin-bottom: 27rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.buy-btn-bg {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border-radius: 44rpx;
}

.buy-btn-text {
  position: relative;
  z-index: 2;
  color: #fff;
  font-size: 32rpx;
  font-weight: 500;
  text-align: center;
  width: 100%;
}

.buy-agree-row {
  display: flex;
  align-items: center;
}

.buy-agree-icon {
  width: 32rpx;
  height: 32rpx;
  margin-right: 12rpx;
}

.buy-agree-text {
  color: #B0B0B0;
  font-size: 23rpx;
}

.buy-agree-link {
  color: #fff;
  font-size: 23rpx;
  margin-left: 6rpx;
}
</style>
